home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Style Layout ƒ / Style Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-18  |  13.9 KB  |  446 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Style Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  11. |**|    "GraphicsLibrary.c", "LayoutLibrary.c", "ShapeLibrary.c",
  12. |**|    TextLibrary.c and "TransformLibrary.c".
  13. |**|
  14. |**|    6/96 bob    Updated #includes to support changed GX Library names.
  15. |**|                Updated the copyright date.
  16. |**|
  17. |**|    ©1990 - 1996  Apple Computer, Inc.
  18. |**|    All rights reserved.
  19. |**|
  20. |**| =====================================================================
  21. \**/
  22.  
  23.  
  24. #include "QDGX shell.h"
  25. #include <GXLayout.h>
  26. #include "LayoutLibrary.h"
  27.  
  28.  
  29. /**\
  30. |**| ---------------------------------------------------------------------
  31. |**| PROTOTYPES
  32. |**| ---------------------------------------------------------------------
  33. \**/
  34.  
  35. // funtions required by shell
  36.  
  37. void    DoSetup            (void);
  38. void    DoDraw            (WindowPtr wind, Boolean updating);
  39. OSErr    DoCreateNew        (void);
  40. void    DoDispose        (WindowPtr wind);
  41. void    DoIdle            (WindowPtr wind);
  42. void    DoTeardown        (void);
  43. void    DoClick            (WindowPtr wind, Point p);
  44.  
  45. // private functions
  46.  
  47. OSErr    DoWindowInit        (WindowPtr wind);
  48. void    CreateSampleImage    (WindowPtr wind);
  49.  
  50.  
  51. /**\
  52. |**| ---------------------------------------------------------------------
  53. |**| ENUMS
  54. |**| ---------------------------------------------------------------------
  55. \**/
  56. enum { rWindResource = 128 };
  57.  
  58.  
  59. /**\
  60. |**| ---------------------------------------------------------------------
  61. |**| GLOBALS
  62. |**| ---------------------------------------------------------------------
  63. \**/
  64. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  65. // functionality will only work with the "debugging" version of QuickDraw GX.
  66. // If the debugging version is not installed, nothing bad will happen, but these
  67. // functions will not work. 
  68.  
  69. Boolean        gDebugging = true;
  70.  
  71. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  72.  
  73. Boolean        gGiveMeValidation = true;
  74.  
  75.  
  76. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  77. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  78. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  79. // since we need abunch if we have several windows open.
  80.  
  81. long        gGraphicsHeapSize = 600;
  82.  
  83. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  84. // override.  This is so that our override can be native PowerPC code if necessary.
  85.  
  86. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  87.  
  88.  
  89.  
  90. /**\
  91. |**| ---------------------------------------------------------------------
  92. |**| DoSetup()
  93. |**| Here's where we initialize any global variables our application needs.
  94. |**| We have only one at this time -- the universal proc pointer for
  95. |**| our printing override.
  96. |**| ---------------------------------------------------------------------
  97. \**/
  98. void DoSetup (void)
  99. {    // Initialize our printing event override UPP
  100.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  101. }
  102.  
  103.  
  104. /**\
  105. |**| ---------------------------------------------------------------------
  106. |**| DoDraw()
  107. |**| Draw the contents of the window.  The first parameter is the window
  108. |**| to draw, and the second parameter is true if we're updating an existing
  109. |**| image.  If that's the case, we don't want to change anything, but
  110. |**| just draw what's already there.
  111. |**| ---------------------------------------------------------------------
  112. \**/
  113. void DoDraw (WindowPtr wind, Boolean updating)
  114. {
  115.      #pragma unused (updating)
  116.      GXDrawShape (GetDocShape(wind));
  117. }
  118.  
  119.  
  120. /**\
  121. |**| ---------------------------------------------------------------------
  122. |**| DoCreateNew()
  123. |**| This routine is called when a window needs to be created.
  124. |**| ---------------------------------------------------------------------
  125. \**/
  126. OSErr DoCreateNew (void)
  127. {
  128.     OSErr        err = noErr;
  129.     WindowPtr    wind;
  130.     
  131. // Get and create our window from the resource fork
  132.  
  133.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  134.  
  135. // Attach a default gxViewPort to it, create and iInitialize our
  136. // private data for it, and add a sample image to its page shape.
  137.  
  138.     if ( wind == NULL )
  139.         return (MemError());
  140.  
  141.     GXIgnoreGraphicsNotice(transform_already_set);
  142.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  143.     GXPopGraphicsNotice();
  144.     
  145.     err = DoWindowInit(wind);
  146.     if ( err != noErr )
  147.         return err;
  148.     
  149.     CreateSampleImage(wind);
  150.     return err;
  151. }
  152.  
  153.  
  154. /**\
  155. |**| ---------------------------------------------------------------------
  156. |**| DoDispose()
  157. |**| This routine is called when a window needs to be disposed of.
  158. |**| ---------------------------------------------------------------------
  159. \**/
  160. void DoDispose (WindowPtr wind)
  161. {
  162.     TH_Doc    doc;
  163.     
  164. // You should always dispose of your GX graphics objects before tossing your window.
  165. // Why?  It's generally good form and this approach guarantees that everything is
  166. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  167. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  168. // with notices set, you will receive a notice that you had not disposed of everything.
  169. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  170.     
  171.     if ( wind != NULL )
  172.     {
  173.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  174.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  175.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  176.         DisposHandle((Handle) doc);            // Dispose of our private data.
  177.         DisposeWindow(wind);                // Dispose of the window.
  178.     }
  179. }
  180.  
  181.  
  182. /**\
  183. |**| ---------------------------------------------------------------------
  184. |**| DoIdle()
  185. |**| This routine is called to do things while idling through the event loop.
  186. |**| ---------------------------------------------------------------------
  187. \**/
  188. void DoIdle (WindowPtr wind)
  189. {
  190. }
  191.  
  192.  
  193. /**\
  194. |**| ---------------------------------------------------------------------
  195. |**| DoTeardown()
  196. |**| This routine is called just before we quit to remove anything 
  197. |**| persistent that might have been setup by DoSetup().
  198. |**| ---------------------------------------------------------------------
  199. \**/
  200. void DoTeardown (void)
  201. {
  202.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  203. }
  204.  
  205.  
  206. /**\
  207. |**| ---------------------------------------------------------------------
  208. |**| DoClick()
  209. |**| ---------------------------------------------------------------------
  210. \**/
  211. void DoClick(WindowPtr window, Point p)
  212. {
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /**\
  220. |**| ---------------------------------------------------------------------
  221. |**| DoWindowInit()
  222. |**| In this function we create and initialize the the private document
  223. |**| structure for a new window.  This structure contains the print job and
  224. |**| the shape which is drawn in the window.  We store this data in a handle
  225. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  226. |**| this, rather than using globals, we can create many windows containing
  227. |**| unique print jobs and shapes.
  228. |**| ---------------------------------------------------------------------
  229. \**/
  230. OSErr DoWindowInit (WindowPtr wind)
  231. {
  232.     OSErr    err = noErr;
  233.     gxJob    docJob;
  234.     gxShape    docPage;
  235.     TH_Doc    windDoc;
  236.  
  237.  
  238. // Create the page shape. We set the unique items attribute to make sure that each item
  239. // added to the picture has a unique reference. If this attribute was not set, we would
  240. // not see all copies of anything we add to the shape multiple times -- we'd just see
  241. // the last version added.        
  242.  
  243.     docPage = GXNewShape(gxPictureType);
  244.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  245.     
  246.     
  247. // Create a print job for this document.  This will be the same as the system default until
  248. // the user goes through the dialogs for Page Setup or Print…
  249.  
  250.     err = GXNewJob(&docJob);
  251.     
  252.     
  253. // If there are no errors, create a handle the size of our document structure and store
  254. // the print job and page shape in it.  Store the handle in the window's refCon field so
  255. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  256. // can be used to do this easily.
  257.  
  258.     if ( err == noErr )
  259.     {
  260.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  261.  
  262.         if ( windDoc == NULL )
  263.             err = MemError();
  264.         else
  265.         {
  266.             (*windDoc)->docJob = docJob;
  267.             (*windDoc)->docPage = docPage;
  268.             SetWRefCon(wind, (long) windDoc);
  269.         }
  270.  
  271. // Now install our application override for PrintingEvent so that we can
  272. // support the new movable-modal printing dialog boxes.
  273.  
  274.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
  275.  
  276.     }
  277.  
  278.     return err;
  279. }
  280.  
  281.  
  282. /**\
  283. |**| ---------------------------------------------------------------------
  284. |**| CreateSampleImage()
  285. |**| This function creates primitive shapes and adds them to the window's page shape.
  286. |**| ---------------------------------------------------------------------
  287. \**/
  288. void CreateSampleImage (WindowPtr wind)
  289. {
  290.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  291.                                             // in QuickDraw coordinates
  292.     gxShape layout;                        // the layout shape we build
  293.     gxShape thePage;                    // this window's document shape
  294.     gxRunControls runControls;            // run controls for the layout shape
  295.     gxLayoutOptions layoutOptions;        // options for the layout shape
  296.     gxStyle timesStyle;                    // a style record for a Times-based style
  297.     gxStyle helveticaStyle;                // ditto for Helvetica
  298.     gxStyle baghdadStyle;                 // ditto for Baghdad
  299.     gxStyle textStyles[5];                // an array of text style for our text runs
  300.     
  301. // These are the five text runs for this layout shape, in pieces because they're
  302. // in different languages
  303.     
  304.     char *text1 = "Aetna ";
  305.     
  306. // The following is "Macintosh" in Arabic: 
  307. // meem, alif, kaf,  noon, tah,  wau,  shin
  308. // (This assumes the standard Arabic character set for Macintosh, by the way)
  309.     
  310.     static char text2[] = {0xE5, 0xC7, 0xE3, 0xE6, 0xCA, 0xE8, 0xD4, 0};
  311.     
  312.     char *text3 = " Office AWAY.";
  313.  
  314.     char *textRuns[3];                    // an array of pointers to text runs
  315.  
  316.     short textLengths[3];                // an array of the lengths of each run
  317.     short totalLength;                    // the total length of the layout's text
  318.     short level0 = 0;                    // a variable to take the address of later
  319.     
  320.     gxPoint posn;                        // the position of the layout shape
  321.  
  322.  
  323. // First, initialize the textRuns array to point to the five text runs
  324.     
  325.     textRuns[0] = text1;
  326.     textRuns[1] = text2;
  327.     textRuns[2] = text3;
  328.     
  329.     
  330. // Next, initialize the textLengths and levelRunLengths arrays.  MyStrLength() is
  331. // in this file.
  332.  
  333.     textLengths[0] = MyStrLength (text1);
  334.     textLengths[1] = MyStrLength (text2);
  335.     textLengths[2] = MyStrLength (text3);
  336.     
  337.     
  338. // totalLength is the length of all the text.
  339.  
  340.     totalLength = textLengths[0] + textLengths[1] + textLengths[2];
  341.     
  342.  
  343. // make default gxLayoutOptions and gxRunControls structures
  344.  
  345.     InitializeLayoutOptions (&layoutOptions);
  346.     InitializeRunControls (&runControls);
  347.     
  348.     
  349. // Position the layout half way down the left edge of the window. Set 
  350. // the layout's width to the window's width and set the flushness to 1/2, this
  351. // will cause the layout to center in the window.  Note that ourWindowRect is
  352. // not in fixed coordinates, so we have to make it fixed to use it.
  353.  
  354.     layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
  355.     layoutOptions.flush = fract1/2;
  356.  
  357.     posn.x = 0;
  358.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  359.     
  360.  
  361. // Create the styles we'll use.  The helveticaStyle is 38-point Helvetica Italics.
  362. // NewLayoutStyle is a library routine found in layout library.c, and
  363. // SetStyleCommonFace found in text library.c.
  364.     
  365.     helveticaStyle = NewLayoutStyle(
  366.         (char *) "\pHelvetica",                // the gxFontName for the style
  367.         ff(38),                                // the text size in fixed point
  368.         0,                                    // gxTextAttributes
  369.         &runControls,                        // run controls (our default ones)
  370.         nil,                                // run features (none for this style)
  371.         0,                                    // count of run features
  372.         nil);                                // style run overrides (none right now)
  373.         
  374.     SetStyleCommonFace(helveticaStyle, italic);
  375.  
  376. // The second style is baghdadStyle -- Baghdad Plain (Arabic), 38 points.
  377.  
  378.     baghdadStyle = NewLayoutStyle(
  379.         (char *) "\pBaghdad Plain",            // gxFontName
  380.         ff(38),                                // text size (fixed point)
  381.         0,                                    // gxTextAttributes
  382.         &runControls,                        // run controls (our default ones)
  383.         nil,                                 // run features (none)
  384.         0,                                     // count of run features (none)
  385.         nil);                                // style run overrides (none)
  386.     
  387. // The final style is 38-point Times Roman with imposed right-to-left direction.
  388.  
  389.     runControls.flags |= gxImposeRightToLeft;
  390.     
  391.     timesStyle = NewLayoutStyle(
  392.         (char *) "\pTimes Roman",             // gxFontName
  393.         ff(38),                             // text size (fixed point)
  394.         0,                                    // gxTextAttributes
  395.         &runControls,                         // run controls (our altered ones)
  396.         nil,                                 // run features (none)
  397.         0,                                     // count of run features (none)
  398.         nil);                                // style run overrides (none)
  399.     
  400.  
  401. // Initialize the textStyles array so that each of the 3 runs has the right style
  402.  
  403.     textStyles[0] = helveticaStyle;
  404.     textStyles[1] = baghdadStyle;
  405.     textStyles[2] = timesStyle;
  406.     
  407.  
  408. // Setup complete!  Build the layout.
  409.  
  410.     layout = GXNewLayout(
  411.         3,                                // count of text runs
  412.         textLengths,                    // array of lengths of each run
  413.         (void *) textRuns,                // array of pointers to the text
  414.         3,                                // count of style runs
  415.         textLengths,                    // array of the byte lengths of each run
  416.         textStyles,                        // array of the styles
  417.         1,                                // number of levels in the layout
  418.         &totalLength,                    // array of the lengths of each level
  419.         &level0,                        // array of the levels
  420.         &layoutOptions,                    // options (default)
  421.         &posn);                            // position of the layout shape
  422.  
  423.     
  424. // Retrieve the page shape so we can add to it.
  425.  
  426.     thePage = GetDocShape(wind);
  427.     
  428. // Add the layout to the window's picture shape.  AddToShape is in shape library.c.
  429.  
  430.     AddToShape(thePage, layout);
  431.  
  432. // Now that the shape is added to the picture, dispose of it and our styles
  433.  
  434.     GXDisposeShape(layout);
  435.     GXDisposeStyle(helveticaStyle);
  436.     GXDisposeStyle(baghdadStyle);
  437.     GXDisposeStyle(timesStyle);
  438.     
  439.  
  440. // Invalidate the window's portRect so that everything gets updated.
  441.     
  442.     SetPort(wind);
  443.     InvalRect(&ourWindowRect);
  444. }
  445.  
  446.